﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace app
{
    partial class Peer
    {
        public byte[] Address = new byte[Parameters.addressSize];

        public IPEndPoint IPEndPoint;

        public Stats Stats;

        public override string ToString()
        {
            return string.Concat(
                string.Join(".", Address),
                ":\t",
                string.Join(".", IPEndPoint.Address.GetAddressBytes()),
                ":\t",
                IPEndPoint.Port);
        }

        public byte[] ToBytes()
        {
            return Address.Concat(
                    Addresses.ToBytes(IPEndPoint)).ToArray();
        }

        public Peer(byte[] address, IPEndPoint iPEndPoint)
        {
            Address = address;

            IPEndPoint = iPEndPoint;

            Stats = new Stats();
        }

        public Peer(byte[] data)
        {
            Address = data.Take(Parameters.addressSize).ToArray();

            IPEndPoint = new IPEndPoint(
                new IPAddress(data.Skip(Parameters.addressSize).Take(Parameters.ipv4Addresssize).ToArray()),
                    BitConverter.ToUInt16(data, Parameters.addressSize + Parameters.ipv4Addresssize));

            Stats = new Stats(data.Skip(Parameters.addressSize + Parameters.requestHeaderSize).Take(10));
        }
    }
}
